import { getUserInfo } from ".";
import { BASE_URL } from "./request";

const uploadFile = (file) => {
  const info = getUserInfo()
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: BASE_URL + '/clock/uploadFile',
      header: {
        authorization: info.token,
        userId: info.userId
      },
      filePath: file.url,
      name: 'file',
      success: (res) => {
        const data = JSON.parse(res.data)
        if (data.code === '0000') {
          resolve(data);
        } else {
          uni.showToast({
            title: data.msg || 'err',
            icon: 'none'
          })
          reject(res);
        }
      },
      fail: (err) => {
        uni.showToast({
          title: err.msg || 'err',
          icon: 'none'
        })
        reject(res);
      },
    })
  })
}

export default uploadFile